package com.drinks.yiniang.common.recycler.decoration;

import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.drinks.yiniang.App;
import com.hansen.library.utils.LogUtils;
import com.hansen.library.utils.ScreenSizeUtils;

/**
 * Created by han on 2019/6/20 0020.
 */

public class AlbumPhotosItemDecoration extends RecyclerView.ItemDecoration {
    private int mSpace;

    public AlbumPhotosItemDecoration(int space) {
        this.mSpace = space;
    }

    public static AlbumPhotosItemDecoration getItemPXDecoration(int space) {
        space = ScreenSizeUtils.dp2px(App.getInstance().getApplicationContext(), space);

        return new AlbumPhotosItemDecoration(space);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();

        //通过layoutposition设置会错乱，改成GridLayoutManager.layoutParams.getSpanIndex()
        GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) view.getLayoutParams();

        int spanCount = layoutManager.getSpanCount();
        int spanSize = params.getSpanSize();

        if(spanSize == 1) {
            outRect.left = (int) (((float) (spanCount - params.getSpanIndex())) / spanCount * mSpace);
            outRect.right = (int) (((float) mSpace * (spanCount + 1) / spanCount) - outRect.left);

            outRect.top = mSpace;
            outRect.bottom = 0;
        } else {
            outRect.left = 0;
            outRect.right = 0;
            outRect.top = 0;
            outRect.bottom = 0;
        }
    }
}
